﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using BO = Viewer.BusinessObject.BusinessEntities;
namespace Viewer.ChucNang
{
    public partial class PhanQuyenChucNang : System.Web.UI.Page
    {
        BO.NSToChuc tochuc = new Viewer.BusinessObject.BusinessEntities.NSToChuc();

        BO.ChucNang query = new Viewer.BusinessObject.BusinessEntities.ChucNang();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataForTree();
                LoadDataControl();
            }
        }
        void LoadDataControl()
        {

            storeToChucCha.DataSource = query.LoadAllChucNang();
            storeToChucCha.DataBind();
        }
        [DirectMethod]
        public void LoadDataForTree()
        {
            TreePanel.SetDataSource(query.LoadAllChucNang());
        }
        [DirectMethod]
        public void LoadGrid(string ID,string nodeName)
        {

            int id;
            try
            {
                id = int.Parse(ID);
            }
            catch (Exception)
            {

                id = 0;
            }
            hdNodeNameModule.Value = nodeName;
            hdIdCha.Value = id;
            storeChucNang.DataSource = query.LoadDataForGrid(id);
            storeChucNang.DataBind();
            grdToChuc.DataBind();
        }
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            int idCha = hdIdCha != null && hdIdCha.Value.ToString() != string.Empty ? Convert.ToInt16(hdIdCha.Value) : 0;
            UcChucNang.ShowDialog("AddNew", 0, idCha);
        }
        [DirectMethod]
        public void ShowDialog(string type, int ID)
        {
            if (type.Equals("Change"))
            {
                UCChangeToChuc.ShowDialogChange(ID, "Chucnang",hdNodeNameModule.Value.ToString());
            }
            else
            {
                int idCha = hdIdCha.Value.ToString() != string.Empty ? Convert.ToInt16(hdIdCha.Value) : 0;
                UcChucNang.ShowDialog(type, ID, idCha);
            }
        }
        protected void btnSearch_Click(object sender, DirectEventArgs e)
        {
            int idNode = !string.IsNullOrEmpty(hdNodeId.Text) ? Convert.ToInt16(hdNodeId.Text) : 0;
            int tochucChaId = !string.IsNullOrEmpty(cbbTochucCha.SelectedItem.Value) ? Convert.ToInt16(cbbTochucCha.SelectedItem.Value) : 0;

            storeChucNang.DataSource = query.SearchByCondition(idNode, txtTenToChuc.Text, tochucChaId);
            storeChucNang.DataBind();
        }
        [DirectMethod]
        public void DeleteChucNang(int id)
        {
            int idCha = hdIdCha.Value.ToString() != string.Empty ? Convert.ToInt16(hdIdCha.Value) : 0;
            query.DeleteChucNang(id);
        
            LoadGrid(idCha.ToString(),"");
            LoadDataForTree();
        }

    }
}
